<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Euclidean projection on a hyperplane</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch08_geometric_probs/html/eucl_proj_hyp.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Euclidean projection on a hyperplane</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.1.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/04/05</span>
<span class="comment">%</span>
<span class="comment">% The projection of x0 on a hyperplane C = {x | a'*x = b} is given by</span>
<span class="comment">%           minimize || x - x0 ||^2</span>
<span class="comment">%               s.t.    a'*x = b</span>
<span class="comment">% It is also given by P_C(x0) = x0 + (b - a'*x0)*a/||a||^2</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n  = 10;
a  = randn(n,1);
b  = randn(1);
x0 = randn(n,1);

<span class="comment">% Analytical solution</span>
fprintf(1,<span class="string">'Computing the analytical solution ...'</span>);
pc_x0 = x0 + (b - a'*x0)*a/norm(a)^2;
fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Solution via QP</span>
fprintf(1,<span class="string">'Computing the optimal solution by solving a QP ...'</span>);

cvx_begin <span class="string">quiet</span>
    variable <span class="string">x(n)</span>
    minimize ( square_pos(norm(x - x0)) )
    a'*x == b;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Verification</span>
disp(<span class="string">'--------------------------------------------------------------------------------'</span>);
disp(<span class="string">'Verifying that p_C(x0) and x_star belong to the hyperplane C: '</span>);
disp([<span class="string">'a^T*p_C(x0) - b = '</span> num2str(a'*pc_x0 - b)]);
disp([<span class="string">'a^T*x_star - b  = '</span> num2str(a'*x - b)]);
disp(<span class="string">'Computing the distance between x0 and the hyperplane in each case'</span>);
disp([<span class="string">'||x0 - p_C(x0)|| = '</span> num2str(norm(x0 - pc_x0))]);
disp([<span class="string">'||x0 - x_star || = '</span> num2str(norm(x0 - x))]);
disp(<span class="string">'Verifying that the analytical solution and the solution obtained via QP are equal: '</span>);
[pc_x0 x]
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the analytical solution ...Done! 
Computing the optimal solution by solving a QP ...Done! 
--------------------------------------------------------------------------------
Verifying that p_C(x0) and x_star belong to the hyperplane C: 
a^T*p_C(x0) - b = -2.2204e-16
a^T*x_star - b  = 4.5741e-14
Computing the distance between x0 and the hyperplane in each case
||x0 - p_C(x0)|| = 0.18218
||x0 - x_star || = 0.18218
Verifying that the analytical solution and the solution obtained via QP are equal: 

ans =

   -0.6316   -0.6316
    1.2834    1.2834
   -0.6345   -0.6345
    0.5984    0.5984
   -0.4016   -0.4016
   -0.0343   -0.0343
   -1.3458   -1.3458
   -1.1631   -1.1631
    1.0003    1.0003
    0.0072    0.0072

</pre>
</div>
</body>
</html>